package org.springblade.modules.digitalization.utils;

import cn.hutool.json.ObjectMapper;
import com.beust.jcommander.internal.Lists;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.wrapper.UserDaRs;
import org.springblade.modules.digitalization.wrapper.UserInfoRs;
import org.springblade.modules.digitalization.wrapper.UserMenuRs;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GbUtils {
	private static final Map<String, String> NUMBER_MAP = new HashMap<>();

	static {
		NUMBER_MAP.put("十", "10");
		NUMBER_MAP.put("一", "1");
		NUMBER_MAP.put("二", "2");
		NUMBER_MAP.put("三", "3");
		NUMBER_MAP.put("五", "5");
		NUMBER_MAP.put("六", "6");
		NUMBER_MAP.put("七", "7");
		NUMBER_MAP.put("八", "8");
	}

	public static String getPreFix(String className, String fen, String numData) {
		String number = NUMBER_MAP.getOrDefault(className, "");

		if (Func.isBlank(number)) {
			// 非数字字符，忽略
			number = className;
		}
		return String.format("%s-%s-%s.jpg", number, fen, numData);
	}

	public static String getXmlByUserReq(UserDaRs useDa) throws JsonProcessingException {
		XmlMapper mapper = new XmlMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		String s = mapper.writeValueAsString(useDa);
		s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + s;
		return s;
	}

	public static void saveXmlByUser(UserDaRs userDaRs, String filePath) throws JsonProcessingException {
		String xmlByUserReq = getXmlByUserReq(userDaRs);
		try (FileWriter fileWriter = new FileWriter(filePath)) {
			fileWriter.write(xmlByUserReq);
			System.out.println("XML 文件写入成功！");
		} catch (Exception e) {
			log.error("XML文件创建失败：" + e.getMessage(), e);
			throw new RuntimeException("国标XML文件创建失败:" + e.getMessage());
		}
	}

	public static UserDaRs xmlUnSerials(String s) throws JAXBException, JsonProcessingException {
		XmlMapper xmlMapper = new XmlMapper();
		UserDaRs root = xmlMapper.readValue(s, UserDaRs.class);
		return root;
	}

	//	public static void main2() throws JsonProcessingException {
//		UserDaRs userDaRs = new UserDaRs();
//		UserInfoRs userInfoRs = new UserInfoRs();
//		userInfoRs.setUserName("李陆军");
//		userInfoRs.setSex("男");
//		userInfoRs.setMz("汉族");
//		userInfoRs.setBirthDay(new Date());
//		UserMenuRs userMenuRs = new UserMenuRs();
//		userMenuRs.setClassNum("一");
//		userMenuRs.setIdNum("1");
//		userMenuRs.setCaiLiaoName("干部履历表");
//		userMenuRs.setCreateAt(new Date());
//		userMenuRs.setPageNum(14);
//		List<String> ys = Lists.newArrayList();
//		List<String> hd = Lists.newArrayList();
//		ys.add("1.jgp");
//		ys.add("2.jgp");
//		ys.add("3.jgp");
//		ys.add("4.jgp");
//		hd.add("1.jgp");
//		hd.add("2.jgp");
//		hd.add("3.jgp");
//		hd.add("4.jgp");
//		userMenuRs.setOrgImgs(ys);
//		userMenuRs.setHdImgs(hd);
//		userDaRs.setUserInfo(userInfoRs);
//		List<UserMenuRs> userMenuRs1 = Lists.newArrayList();
//		userMenuRs1.add(userMenuRs);
//		userDaRs.setMenus(userMenuRs1);
//		System.out.println(getXmlByUserReq(userDaRs));
//		saveXmlByUser(userDaRs);
//	}
	public static void main(String[] args) throws IOException {
		Path oldFolderPath = Paths.get("E:\\receiveDa\\web\\gbPath\\3f797134-4b50-45c0-a219-ce29e460de95");
		String newFolderName = "李浩418475199651471543";
		Files.move(oldFolderPath, oldFolderPath.resolveSibling(newFolderName), StandardCopyOption.REPLACE_EXISTING);
	}
}
